package com.auto.survey.widget

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewTreeObserver
import android.view.animation.LinearInterpolator
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.auto.survey.R
import com.auto.survey.base.CustomApplication

class CentreToast private constructor(context: Context): Toast(context) {

    // 单例实例持有者
    companion object {
        val instance: CentreToast by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
            Holder.INSTANCE
        }
    }

    // 静态内部类持有单例实例
    private object Holder {
        @SuppressLint("StaticFieldLeak")
        val INSTANCE = CentreToast(CustomApplication.getContext())
    }

    private var imageView:ImageView?=null
    private var textView:TextView?=null
    private var rotateAnimator: ObjectAnimator? = null

    // 初始化自定义布局
    init {
        val view = LayoutInflater.from(context).inflate(R.layout.toast_center, null)
        imageView = view.findViewById(R.id.toast_img)
        textView = view.findViewById(R.id.toast_text)
        setView(view)
        setGravity(Gravity.CENTER, 0, 0) // 居中显示
    }


    /**
     * 显示一个纯文本吐司
     *
     * @param context 上下文
     * @param text    显示的文本
     */
    fun showText(message: String) {
        imageView?.visibility = View.GONE
        textView?.apply {
            visibility = View.VISIBLE
            this.text = message
        }

        this.duration = duration
        show()
    }


    fun show (message: String, imgType: Boolean) {
        show(message, LENGTH_SHORT, imgType);
    }

    // 简化显示方法
    fun show(message: String, duration: Int = LENGTH_SHORT, imgType:Boolean) {
        textView?.text = message
        imageView?.visibility = View.VISIBLE
        if (imgType) {
            imageView?.setBackgroundResource(R.mipmap.toast_y)
        } else {
            imageView?.setBackgroundResource(R.mipmap.toast_n)
        }

        this.duration = duration

        // 创建或重置旋转动画
        cancelAnimation()  // 取消之前的动画
        rotateAnimator = ObjectAnimator.ofFloat(imageView, "rotationY", 0f, 360f).apply {
            this.duration = 1000
            interpolator = LinearInterpolator()
            start()
        }

        // 设置 Toast 消失时的监听器
        view?.viewTreeObserver?.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
            override fun onPreDraw(): Boolean {
                if (view?.windowToken == null) {
                    // Toast 消失时，取消动画并释放资源
                    cancelAnimation()
                    view?.viewTreeObserver?.removeOnPreDrawListener(this)
                }
                return true
            }
        })

        show()
    }

    // 取消动画
    private fun cancelAnimation() {
        rotateAnimator?.cancel()
        rotateAnimator = null
    }

    // 重写 cancel() 方法
    override fun cancel() {
        cancelAnimation()
        super.cancel()
    }

}